| Author | Manuela Ruiz (mruiz@lcc.uma.es) |
This class represents a segment, with its end points
| tail | the first point of the segment |
| head | the second point of the segment |
Initialize the segment, ordering the tail and head propertly, so tail < head
# File lib/geometry.rb, line 302
302: def initialize(tail, head)
303: #Order the tail and head
304: if tail < head
305: @tail = tail
306: @head = head
307: else
308: @tail = head
309: @head = tail
310: end
311:
312: #Compute the line descriptor
313: if (@tail.x - @head.x).abs < Constants::EPSILON #Vertical
314: sine = 1
315: intercept = @tail.x
316: elsif (@tail.y - @head.y).abs < Constants::EPSILON #Horizontal
317: sine = 0
318: intercept = @tail.y
319: else
320: sine = ((@head.y - @tail.y) / Math.sqrt(((@head.x - @tail.x)**2 )+((@head.y - @tail.y)**2)))
321: if ((@head.x - @tail.x) > 0)
322: den = Math.sqrt((@head.x - @tail.x)**2)
323: else
324: den = 1 * Math.sqrt((@head.x - @tail.x)**2)
325: end
326: if ((@head.y - @tail.y) > 0)
327: mult = Math.sqrt((@head.y - @tail.y)**2)
328: else
329: mult = 1 * Math.sqrt((@head.y - @tail.y)**2)
330: end
331: intercept = (1*@tail.x/den)*(mult) + @tail.y
332: end
333: @line_descriptor = LineDescriptor.new(sine, intercept)
334: end
| other_segment | another segment |
| returns | true iff this segment is lesser than other_segment. Only collinear segments can be compared. |
A segment A is lesser than a segment B iff the head of A is smaller than the tail of B.
# File lib/geometry.rb, line 459
459: def < (other_segment)
460: raise ArgumentError, 'The argument is not of type Segment' unless other_segment.kind_of? Segment
461: #raise ArgumentError, 'Segments are not collinear' unless collinear?(other_segment)
462: return @head < other_segment.tail
463: end
| other_segment | another segment |
| returns | true iff this segment is equal to other_segment. Only collinear segments can be compared. |
# File lib/geometry.rb, line 468
468: def == (other_segment)
469: result = false
470: if other_segment.kind_of? Segment
471: result = ((@head == other_segment.head) && (@tail == other_segment.tail))
472: end
473: return result
474: end
| returns | a new Segment object, identical to this one |
# File lib/geometry.rb, line 477
477: def clone()
478: return Segment.new(@tail.clone, @head.clone)
479: end
| point | a point |
| returns | true iff the point is coincident with the segment, that is, is ‘inside’ the segment |
# File lib/geometry.rb, line 411
411: def coincident?(point)
412: result = false
413:
414: if (point == @tail) || (point == @head)
415: result = true
416: elsif (@line_descriptor.satisfied? point) && (@tail < point) && (point < @head)
417: result = true
418: end
419:
420: return result
421: end
| other_segment | another segment |
| returns | true iff both line descriptors are equal, that is, both segments share the same ‘mother line’ |
# File lib/geometry.rb, line 450
450: def collinear?(other_segment)
451: raise ArgumentError, 'The argument is not of type Segment' unless other_segment.kind_of? Segment
452: return @line_descriptor == other_segment.line_descriptor
453: end
| returns | the hash code for this segment |
# File lib/geometry.rb, line 482
482: def hash
483: return [@line_descriptor.hash, @tail.hash, @head.hash].hash
484: end
| new_head | an OrderedPoint |
Sets the head to new_head
# File lib/geometry.rb, line 375
375: def head=(new_head)
376: #Order the tail and head
377: if @tail < new_head
378: @head = new_head
379: else
380: @head = @tail
381: @tail = new_head
382: end
383:
384: #Compute the line descriptor
385: if (@tail.x - @head.x).abs < Constants::EPSILON #Vertical
386: sine = 1
387: intercept = @tail.x
388: elsif (@tail.y - @head.y).abs < Constants::EPSILON #Horizontal
389: sine = 0
390: intercept = @tail.y
391: else
392: sine = ((@head.y - @tail.y) / Math.sqrt(((@head.x - @tail.x)**2)+((@head.y - @tail.y)**2)))
393: if ((@head.x - @tail.x) > 0)
394: den = Math.sqrt((@head.x - @tail.x)**2)
395: else
396: den = 1 * Math.sqrt((@head.x - @tail.x)**2)
397: end
398: if ((@head.y - @tail.y) > 0)
399: mult = Math.sqrt((@head.y - @tail.y)**2)
400: else
401: mult = 1 * Math.sqrt((@head.y - @tail.y)**2)
402: end
403: intercept = (1*@tail.x/den)*(mult) + @tail.y
404: end
405: @line_descriptor = LineDescriptor.new(sine, intercept)
406: end
| returns | the distance between the tail and the head of this segment |
# File lib/geometry.rb, line 487
487: def length
488: return @tail.point.distance(@head.point)
489: end
| other_segment | another segment |
| returns | the resulting segment in case the two segments overlap. |
# File lib/geometry.rb, line 426
426: def overlap?(other_segment)
427: raise ArgumentError, 'The argument is not of type Segment' unless other_segment.kind_of? Segment
428: result = nil
429: if collinear?(other_segment)
430: if (@tail < other_segment.head) && (other_segment.tail < @head)
431: new_tail = @tail
432: if @tail < other_segment.tail
433: new_tail = other_segment.tail
434: end
435:
436: new_head = @head
437: if other_segment.head < @head
438: new_head = other_segment.head
439: end
440:
441: result = Segment.new(new_tail, new_head)
442: end
443: end
444: return result
445: end
| new_tail | an OrderedPoint |
Sets the tail to new_tail
# File lib/geometry.rb, line 339
339: def tail=(new_tail)
340: #Order the tail and head
341: if new_tail < @head
342: @tail = new_tail
343: else
344: @tail = @head
345: @head = new_tail
346: end
347:
348: #Compute the line descriptor
349: if (@tail.x - @head.x).abs < Constants::EPSILON #Vertical
350: sine = 1
351: intercept = @tail.x
352: elsif (@tail.y - @head.y).abs < Constants::EPSILON#Horizontal
353: sine = 0
354: intercept = @tail.y
355: else
356: sine = ((@head.y - @tail.y) / Math.sqrt(((@head.x - @tail.x)**2 )+((@head.y - @tail.y)**2)))
357: if ((@head.x - @tail.x) > 0)
358: den = Math.sqrt((@head.x - @tail.x)**2)
359: else
360: den = 1 * Math.sqrt((@head.x - @tail.x)**2)
361: end
362: if ((@head.y - @tail.y) > 0)
363: mult = Math.sqrt((@head.y - @tail.y)**2)
364: else
365: mult = 1 * Math.sqrt((@head.y - @tail.y)**2)
366: end
367: intercept = (1*@tail.x/den)*(mult) + @tail.y
368: end
369: @line_descriptor = LineDescriptor.new(sine, intercept)
370: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.